package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.entity.category;
import com.reggie.service.categoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping(value = "/category")
@Transactional
public class categoryController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private categoryService categoryService;

    /**
     * @param category
     * @return com.reggie.common.R<java.lang.String>
     * @description: 保存新的菜品
     * @author 86187
     * @date: 2022/11/17 11:07
     */

    @PostMapping()
    public R<String> saveDishes(@RequestBody category category) {
        log.info("保存新的菜品:{}", category);
        boolean save = categoryService.save(category);
        if (save) {
            //清楚缓存
            redisTemplate.delete("categoryPage");
            return R.success("新的菜品保存成功!");
        } else {
            return R.error("新的菜品保存失败!");
        }
    }

    /**
     * @param page
     * @param pageSize
     * @return com.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     * @description: 返回菜品和套餐列表数据
     * @author 86187
     * @date: 2022/11/17 11:33
     */

    @GetMapping("/page")
    public R<Page> selectAllCategoryByCondition(int page, int pageSize) {
        //判断是否存在redis缓存
        Page<category> categoryPage = (Page<category>) redisTemplate.opsForValue().get("categoryPage");
        if (categoryPage != null) {
            return R.success(categoryPage);
        }
        Page<category> pageInfo = new Page<>(page, pageSize);
        categoryService.page(pageInfo);
        //存入redis
        redisTemplate.opsForValue().set("categoryPage", pageInfo, 60, TimeUnit.MINUTES);
        return R.success(pageInfo);
    }

    /**
     * @param category
     * @param session
     * @return com.reggie.common.R<java.lang.String>
     * @description: 修改菜品
     * @author 86187
     * @date: 2022/11/17 11:38
     */

    @PutMapping()
    public R<String> updateCategory(@RequestBody category category, HttpSession session) {
        log.info("{}修改了{}菜品", session.getAttribute("employee"), category.getName());
        boolean b = categoryService.updateById(category);
        if (b) {
            //清楚相应菜品类型的redis缓存
            redisTemplate.delete("categoryPage");
            return R.success("菜品修改成功!");
        } else {
            return R.error("菜品修改失败!");
        }
    }

    /**
     * @param ids
     * @param session
     * @return com.reggie.common.R<java.lang.String>
     * @description: 删除菜品
     * @author 86187
     * @date: 2022/11/17 11:46
     */

    @DeleteMapping()
    public R<String> deleteCategory(@RequestParam("ids") Long ids, HttpSession session) {
        LambdaQueryWrapper<category> queryWrapper = new LambdaQueryWrapper<>();
        log.info("{}删除了{}菜品", session.getAttribute("employee"), ids);
        queryWrapper.in(ids != null, category::getId, ids);
        boolean b = categoryService.removeById(ids);
        if (b) {
            redisTemplate.delete("categoryPage");
            return R.success("删除成功!");
        } else {
            return R.error("删除失败!");
        }
    }

    /**
     * @param type
     * @return com.reggie.common.R<java.util.List < com.reggie.entity.category>>
     * @description: 返回添加菜品所需要的分类
     * @author 86187
     * @date: 2022/11/18 14:17
     */

    @GetMapping("/list")
    public R<List<category>> queryCategoryForAddDish(@RequestParam(value = "type", required = false) Integer type) {
        //先查询redis缓存是否存在
        List<category> categoryList = (List<category>) redisTemplate.opsForValue().get("category:" + type);
        if (categoryList != null) {
            return R.success(categoryList);
        }
        LambdaQueryWrapper<category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, category::getType, type);
        List<category> TypeList = categoryService.list(queryWrapper);
        //存入redis缓存中
        redisTemplate.opsForValue().set("category:" + type, TypeList, 60, TimeUnit.MINUTES);
        return R.success(TypeList);
    }
}
